package com.dhcc.controller;

import com.dhcc.api.R;
import com.dhcc.dto.SysDepartmentDto;
import com.dhcc.entity.SysDepartment;
import com.dhcc.httpApiService.IdGeneratorService;
import com.dhcc.service.ClientInstitutionRoleDepartmentService;
import com.dhcc.service.SysDepartmentService;
import com.dhcc.vo.SysDepartmentTree;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userSystem/clientDepartment")
public class SysDepartmentController {
    @Resource
    private SysDepartmentService sysDepartmentService;
    @Resource
    private IdGeneratorService idGeneratorService;
    @Resource
    private ClientInstitutionRoleDepartmentService clientInstitutionRoleDepartmentService;
    /**
     * 通过机构编号查询当前机构下的部门
     */
    @GetMapping("/getDepartmentList")
    public R<List<String>> getDepartmentList(String institutionId,String institutionRoleId){
        return R.data(sysDepartmentService.getDepartmentList(institutionId,institutionRoleId));
    }
    /**
     * 查询当前机构下的部门列表树
     */
    @GetMapping("/selectDepartmentListTree")
    public R<List<SysDepartmentTree>> selectDepartmentListTree(String institutionId, String institutionRoleId){
       return R.data(sysDepartmentService.selectDepartmentListTree(institutionId,institutionRoleId));
    }
    /**
     * 查询当前机构下的部门列表
     */
    @GetMapping("/selectDepartmentList")
    public R<List<SysDepartment>> selectDepartmentList(String institutionId, String institutionRoleId){
        return R.data(sysDepartmentService.selectDepartmentList(institutionId,institutionRoleId));
    }
    /**
     * 部门详情
     */
    @GetMapping("/getDepartmentInfo")
    public R<SysDepartment> getDepartmentInfo(String departmentId){
        return R.data(sysDepartmentService.getDepatment(departmentId));
    }
    /**
     * 添加部门
     */
    @PostMapping("/addDepartment")
    @Transactional
    public R addDepartment(@RequestBody SysDepartmentDto sysDepartmentDto){
        SysDepartment sysDepartment=new SysDepartment();
        BeanUtils.copyProperties(sysDepartmentDto,sysDepartment);
        Long aLong = idGeneratorService.nextId();
        if (sysDepartmentDto.getParementId()==""){
            sysDepartment.setParementId(null);
        }
        sysDepartment.setDepartmentId(aLong.toString());
        sysDepartment.setIsDeleted(0);
        Integer addResult= sysDepartmentService.addDepartment(sysDepartment);
        if (addResult!=1){
            throw new RuntimeException("数据库添加失败");
        }
        //在机构，机构角色，部门关联表中添加对应的信息
        Integer result= clientInstitutionRoleDepartmentService.addInforment(aLong.toString(),sysDepartmentDto.getInstitutionId(),sysDepartmentDto.getInstitutionRoleId());
        if (result!=1){
            throw new RuntimeException("机构，机构角色，部门关联表中添加数据失败");
        }
        return R.success();
    }
    /**
     * 部门删除
     */
    @GetMapping("/deleteDepartment")
    @Transactional
    public R deleteDepartment(String institutionId,String institutionRoleId,String departmentId){
        //查询当前部门是否含有子部门
        List<SysDepartment> sysDepartmentList=sysDepartmentService.selectdepartmentListByFatherDeptId(departmentId);
        if (sysDepartmentList.size()!=0){
            return R.fail("该部门下还有未删除的子部门，请先删除子部门！");
        }
        //查询当前机构下的所有部门
        List<SysDepartment> sysDepartments = sysDepartmentService.selectDepartmentList(institutionId, institutionRoleId);
        if (sysDepartments.size()<=1){
            return R.fail("请至少保留一个部门！");
        }
        //删除部门
        Integer integer = sysDepartmentService.deleteDepartment(departmentId);
        if (integer!=1){
            return R.fail("部门删除失败");
        }
        //删除机构，机构编号，部门关联表中的数据
        Integer result= clientInstitutionRoleDepartmentService.deleteData(institutionId,institutionRoleId,departmentId);
        if (result!=1){
            return R.fail("机构，机构编号，部门关联表数据删除失败");
        }
        return R.success();
    }
    /**
     * 部门修改
     */
    @PostMapping("/updateDepartment")
    @Transactional
    public R updateDepartment(@RequestBody SysDepartment sysDepartment){
        Integer result= sysDepartmentService.updateDepartment(sysDepartment);
        if (result!=1){
            R.fail("更新失败");
        }
        return R.success();
    }
}
